package cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.impl;

import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.util.PageInfoToPageDataConverter;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.mapper.AppointmentMapper;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IAppointmentRepository;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.Appointment;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.AppointmentListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.ClinicListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Repository
public class AppointmentRepositoryImpl implements IAppointmentRepository {

    @Autowired
    private AppointmentMapper appointmentMapper;

    public AppointmentRepositoryImpl() {
        log.info("创建存储库对象:AppointmentRepositoryImpl");
    }

    @Override
    public int insert(Appointment appointment) {
        log.debug("开始向[预约挂号表]中插入数据:{}",appointment);
        return appointmentMapper.insert(appointment);
    }

    @Override
    public PageData<AppointmentListVO> listByCreateTime(Integer pageNum, Integer pageSize, String createTime) {
        log.debug("开始执行【根据预约时间查询预约单】，页码：{}，每页记录数：{},预约时间:{}", pageNum, pageSize,createTime);
        PageHelper.startPage(pageNum, pageSize);
        List<AppointmentListVO> list = appointmentMapper.listByCreateTime(LocalDateTime.parse(createTime));
        PageInfo<AppointmentListVO> pageInfo = new PageInfo<>(list);
        PageData<AppointmentListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<AppointmentListVO> listByVisitTime(Integer pageNum, Integer pageSize, String visitTime) {
        log.debug("开始执行【根据来访时间查询预约单】，页码：{}，每页记录数：{},问诊时间:{}", pageNum, pageSize,visitTime);
        PageHelper.startPage(pageNum, pageSize);
        List<AppointmentListVO> list = appointmentMapper.listByVisitTime(LocalDateTime.parse(visitTime));
        PageInfo<AppointmentListVO> pageInfo = new PageInfo<>(list);
        PageData<AppointmentListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<AppointmentListVO> listByDoctorName(Integer pageNum, Integer pageSize, String doctorName) {
        log.debug("开始执行【根据医生姓名查询预约单】，页码：{}，每页记录数：{},医生姓名:{}", pageNum, pageSize,doctorName);
        PageHelper.startPage(pageNum, pageSize);
        List<AppointmentListVO> list = appointmentMapper.listByDoctorName(doctorName);
        PageInfo<AppointmentListVO> pageInfo = new PageInfo<>(list);
        PageData<AppointmentListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<AppointmentListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行[查询预约单列表]:页码{},页数:{}",pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<AppointmentListVO> list = appointmentMapper.listAppointment();
        PageInfo<AppointmentListVO> pageInfo = new PageInfo<>(list);
        PageData<AppointmentListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
